﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GenerateurMusique
{
    /// <summary>
    /// Logique d'interaction pour musicWindow.xaml
    /// </summary>
    public partial class musicWindow : Window
    {
        Population myPopulation;
        public static Boolean isPlaying;
        public static MediaPlayer mplayer = new MediaPlayer();
        public musicWindow()
        {
            InitializeComponent();
        }

        private void btSaveClick(int nb)
        {
            myPopulation.tabIndividus[nb].saveMidiSong();
        }

        private void btPlayClick(int nb)
        {
            myPopulation.tabIndividus[nb].playMidiSong(nb);           
        }
        
        private void btBegin_Click(object sender, RoutedEventArgs e)
        {
            myPopulation = new Population();
            txtGeneration.Text = "Generation n°" + myPopulation.countGen;
            unlockButtonsAndClearText();
        }

        private void btValider_Click(object sender, RoutedEventArgs e)
        {
            if(this.CheckFormatTextbox()){
                if (isPlaying)
                {
                    mplayer.Stop();
                    mplayer.Close();
                    isPlaying = false;
                }
                this.parseAndSetTickets();
                lockButtons();
                myPopulation.selectionReproducteur();
                myPopulation.reproduction();
                myPopulation.individuSurvie();
                eraseSong();
                unlockButtonsAndClearText();
                txtGeneration.Text = "Generation n°" + myPopulation.countGen;
            }else{
                MessageBox.Show("Veuillez donner une note (entier entre 0 et 10) pour tout les sons.","Erreur Format",MessageBoxButton.OK,MessageBoxImage.Exclamation);
            }
        }

        /// <summary>
        /// Erase all the temporary midi file
        /// </summary>
        private void eraseSong()
        {
            if (isPlaying)
            {
                mplayer.Stop();
                mplayer.Close();
                isPlaying = false;
            }
            var files = Directory.EnumerateFiles("./", "Fichier*.mid");
            foreach (string file in files)
            {
                File.Delete(file);
            }
        }

        /// <summary>
        /// Parse and Set the ticket of the population with the value of the textBlock
        /// </summary>
        private void parseAndSetTickets()
        {
            myPopulation.tabIndividus[0].setTickets(int.Parse(txtTicketOne.Text));
            myPopulation.tabIndividus[1].setTickets(int.Parse(txtTicketTwo.Text));
            myPopulation.tabIndividus[2].setTickets(int.Parse(txtTicketThree.Text));
            myPopulation.tabIndividus[3].setTickets(int.Parse(txtTicketFour.Text));
            myPopulation.tabIndividus[4].setTickets(int.Parse(txtTicketFive.Text));
            myPopulation.tabIndividus[5].setTickets(int.Parse(txtTicketSix.Text));
            myPopulation.tabIndividus[6].setTickets(int.Parse(txtTicketSeven.Text));
            myPopulation.tabIndividus[7].setTickets(int.Parse(txtTicketHeight.Text));
            myPopulation.tabIndividus[8].setTickets(int.Parse(txtTicketNine.Text));
            myPopulation.tabIndividus[9].setTickets(int.Parse(txtTicketTen.Text));
        }

        /// <summary>
        /// Check if the format of all the text is correct
        /// </summary>
        /// <returns>True if all is correct</returns>
        private Boolean CheckFormatTextbox()
        {
            Boolean isCorrect = true;
            int value1;

            if (! int.TryParse(txtTicketOne.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketOne.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketTwo.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketTwo.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketThree.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketThree.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketFour.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketFour.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketFive.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketFive.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketSix.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketSix.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketHeight.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketHeight.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketNine.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketNine.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            if (! int.TryParse(txtTicketTen.Text, out value1)){
                isCorrect = false;
            }else if (int.TryParse(txtTicketTen.Text, out value1)){
                if (value1 < 0 || value1 > 10)
                {
                    isCorrect = false;
                }
            }

            return isCorrect;
        }

        /// <summary>
        /// Lock the buttons for a procedur 
        /// </summary>
        public void lockButtons()
        {
            btBegin.IsEnabled = false;
            btPlay1.IsEnabled = false;
            btSave1.IsEnabled = false;
            btPlay2.IsEnabled = false;
            btSave2.IsEnabled = false;
            btPlay3.IsEnabled = false;
            btSave3.IsEnabled = false;
            btPlay4.IsEnabled = false;
            btSave4.IsEnabled = false;
            btPlay5.IsEnabled = false;
            btSave5.IsEnabled = false;
            btPlay6.IsEnabled = false;
            btSave6.IsEnabled = false;
            btPlay7.IsEnabled = false;
            btSave7.IsEnabled = false;
            btPlay8.IsEnabled = false;
            btSave8.IsEnabled = false;
            btPlay9.IsEnabled = false;
            btSave9.IsEnabled = false;
            btPlay10.IsEnabled = false;
            btSave10.IsEnabled = false;
            btValider.IsEnabled = false;
        }

        /// <summary>
        /// Unlock all the buttons and clear the textblock
        /// </summary>
        public void unlockButtonsAndClearText()
        {
            btPlay1.IsEnabled = true;
            btSave1.IsEnabled = true;
            btPlay2.IsEnabled = true;
            btSave2.IsEnabled = true;
            btPlay3.IsEnabled = true;
            btSave3.IsEnabled = true;
            btPlay4.IsEnabled = true;
            btSave4.IsEnabled = true;
            btPlay5.IsEnabled = true;
            btSave5.IsEnabled = true;
            btPlay6.IsEnabled = true;
            btSave6.IsEnabled = true;
            btPlay7.IsEnabled = true;
            btSave7.IsEnabled = true;
            btPlay8.IsEnabled = true;
            btSave8.IsEnabled = true;
            btPlay9.IsEnabled = true;
            btSave9.IsEnabled = true;
            btPlay10.IsEnabled = true;
            btSave10.IsEnabled = true;
            btValider.IsEnabled = true;
            txtTicketOne.Text = "";
            txtTicketTwo.Text = "";
            txtTicketThree.Text = "";
            txtTicketFour.Text = "";
            txtTicketFive.Text = "";
            txtTicketSix.Text = "";
            txtTicketSeven.Text = "";
            txtTicketHeight.Text = "";
            txtTicketNine.Text = "";
            txtTicketTen.Text = "";
        }

        private void btPlay2_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(1);
        }

        private void btPlay1_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(0);
        }

        private void btSave1_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(0);
        }

        private void btSave2_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(1);
        }

        private void btPlay3_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(2);
        }

        private void btSave3_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(2);
        }

        private void btPlay4_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(4-1);
        }

        private void btSave4_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(4 - 1);
        }

        private void btPlay5_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(5 - 1);
        }

        private void btSave5_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(5 - 1);
        }

        private void btPlay6_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(6 - 1);
        }

        private void btSave6_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(6 - 1);
        }

        private void btPlay7_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(7 - 1);
        }

        private void btSave7_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(7 - 1);
        }

        private void btPlay8_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(8 - 1);
        }

        private void btSave8_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(8 - 1);
        }

        private void btPlay9_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(9 - 1);
        }

        private void btSave9_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(9 - 1);
        }

        private void btPlay10_Click(object sender, RoutedEventArgs e)
        {
            btPlayClick(10 - 1);
        }

        private void btSave10_Click(object sender, RoutedEventArgs e)
        {
            btSaveClick(10 - 1);
        }

        private void menu_Closed(object sender, EventArgs e)
        {
            eraseSong();
        }
    }
}
